package home_pagePO;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;


public class SearchPO {
    AppiumDriver driver;
//    AndroidDriver driver;

    public SearchPO(AppiumDriver driver) {
        this.driver = driver;
    }


    public static int size;


    public static By tv_title = By.id("com.ksbao.nursingstaffs:id/tv_title");
    public static int X = 442;
    public static int Y = 301;
    public static By Search_btn = By.id("com.ksbao.nursingstaffs:id/tv_search");
    public static By search_txt = By.id("com.ksbao.nursingstaffs:id/et_search");
    //模糊搜索到的列表
    public static By search_List = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View[2]/android.view.View");
    //首页当前科目text
    public static By  title_text = By.id("com.ksbao.nursingstaffs:id/tv_title");
    //广告关闭
    public static By advertisement = By.id("com.ksbao.nursingstaffs:id/img_btn_cancel");

    //程序一共等待5s，每0.5秒检查一次元素是否加载完成，其实后面的0.5可以省略，因为默认检测频率为0.5s

    public SearchPO home_Search(String search) throws Exception {
        WebDriverWait wait = new WebDriverWait(driver, 5, (long) 0.5);
        //顶部切换科目
        driver.findElement(tv_title).click();
        Thread.sleep(2000);

        TouchAction touchAction = new TouchAction(driver);
//        touchAction.tap(title_text,400,300).release().perform();
        //坐标点击
        touchAction.tap(PointOption.point(X, Y)).perform();

        Thread.sleep(1000);

        driver.findElement(Search_btn).click();
        Thread.sleep(1000);

        driver.findElement(search_txt).sendKeys(search);
        Thread.sleep(1000);

        size = driver.findElements(search_List).size();
        System.out.println("搜索到" + size + "个科目");

        //列表随机数
        int ran_search = 1 + (int) (Math.random() * size);
        By search_click = By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View[2]/android.view.View[" + ran_search + "]/android.widget.TextView");
        //获取选择的科目名称
        String text = driver.findElement(search_click).getText();
        System.out.println("选择了" + text);
        driver.findElement(search_click).click();

        wait.until(ExpectedConditions.presenceOfElementLocated((advertisement))).click();

        String home_title = driver.findElement(title_text).getText();
        System.out.println("当前首页显示的科目" + home_title);


        try {
            Assert.assertEquals(home_title, text);
            System.out.println("选择成功，进到首页");
        } catch (AssertionError e) {
            System.out.println("预期与实际不符合" + e);
        }


        return new SearchPO(driver);
    }


}
